package cn.ai.shoes.core.utils;

import cn.ai.shoes.core.pojo.entity.MyUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Spring Security 工具类
 */
public class SecurityUtils {
    
    /**
     * 获取当前登录用户
     * @return 当前登录用户
     */
    public static MyUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }
        Object principal = authentication.getPrincipal();
        if (principal instanceof MyUser) {
            return (MyUser) principal;
        }
        return null;
    }

    /**
     * 获取当前登录用户名
     * @return 当前登录用户名
     */
    public static String getCurrentUsername() {
        MyUser user = getCurrentUser();
        return user != null ? user.getUsername() : null;
    }

    /**
     * 判断当前用户是否已登录
     * @return 是否已登录
     */
    public static boolean isAuthenticated() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication != null && authentication.isAuthenticated();
    }
} 